﻿
'Bram Debouvere
'Clinciu Andrei

Option Strict On

Imports System.IO
Imports Gift.BL
Imports System.Configuration
Imports Microsoft.WindowsAPICodePack.Taskbar


Public Class frmMain


    Public dbh As DatabaseHelper
    Private _personForm As frmPersons = Nothing
    Private _productsForm As frmProducts = Nothing
    Private _giftListsForm As frmGiftLists = Nothing
    Private _orderForm As frmOrders = Nothing

    'icons for win7
    Public WithEvents btnProducten As New ThumbnailToolbarButton(My.Resources.producten, "Producten")
    Public WithEvents btnBestellingen As New ThumbnailToolbarButton(My.Resources.bestellingen, "Bestellingen")
    Public WithEvents btnPersonen As New ThumbnailToolbarButton(My.Resources.personen, "Personen")
    Public WithEvents btnGeschenkenlijsten As New ThumbnailToolbarButton(My.Resources.geschenkenlijsten, "Geschenkenlijsten")


    Private Sub startDbh()
        Try
            dbh = New DatabaseHelper()
        Catch ex As Exception
            MessageBox.Show("Het programma kan geen verbinding maken met de database-server." & vbCrLf & "Zorg dat de databaseserver aan staat en dat de database bestaat" & vbCrLf & "Contacteer uw systeembeheerder indien dit probleem zich blijft voordoen." & vbCrLf & "Het programma wordt nu afgesloten.", "Fout bij connectie naar database", MessageBoxButtons.OK, MessageBoxIcon.Stop)
            Close()
        End Try
    End Sub

    Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'open the database and use frmMain.dbh to give it from the forms to the database.. we close it when the form ends
        startDbh()
        'add icons for win7
        Dim array(3) As ThumbnailToolbarButton
        array(1) = btnProducten
        array(2) = btnBestellingen
        array(3) = btnPersonen
        array(0) = btnGeschenkenlijsten
        TaskbarManager.Instance.ThumbnailToolbars.AddButtons(Me.Handle, array)
        'Checking settings & loading them
        CheckSettings()
    End Sub


    'Settings functions looking after the settings.xml & creating it
    'Using new .NET functionality: LINQ XML
    'Used for imagesfolder at the moment, can be extended for other things as well
    Private Sub CheckSettings()
        Dim settingsPath As String = My.Application.Info.DirectoryPath & "\settings.xml"
        Dim changedSettings As Boolean = False

        If Not File.Exists(settingsPath) Then
            Dim settingsXML As XElement =
              <settings>
                  <imagesFolder>none</imagesFolder>
                  <firmName>none</firmName>
              </settings>
            'one line save xml
            settingsXML.Save(settingsPath)
        End If
        'one line load
        Dim settings = XDocument.Load(settingsPath)
        Console.WriteLine(settings)

        'This means no folder exists
        If settings.<settings>.<imagesFolder>.Value = "none" Then
            Console.WriteLine("No  folder chosen.. chose one please")
            fbd.Description = "Kies een folder met images voor uw producten"

            If fbd.ShowDialog = Windows.Forms.DialogResult.OK Then
                Console.WriteLine("Chosen folder..")
                'xml get path
                settings.<settings>.<imagesFolder>.Value = fbd.SelectedPath
                Console.WriteLine(fbd.SelectedPath)
                Console.WriteLine(settings)
                changedSettings = True
            Else
                Dim newDir As String = Application.StartupPath & "\images"
                System.IO.Directory.CreateDirectory(newDir)
                settings.<settings>.<imagesFolder>.Value = newDir
                changedSettings = True
            End If
        End If

        'Save the settings only if something changed
        If changedSettings = True Then
            settings.Save(settingsPath)
        End If

        'check firm value
        If settings.<settings>.<firmName>.Value = "none" Then
            Dim dialog As New frmFirmDetails
            'add the information to the file
            If dialog.ShowDialog() = Windows.Forms.DialogResult.OK Then
                settings.<settings>.<firmName>.Value = dialog.txtBedrijfnaam.Text
                settings.Root.Add(<btwNummer></btwNummer>)
                settings.<settings>.<btwNummer>.Value = dialog.txtBtw.Text
                settings.Root.Add(<telefoon></telefoon>)
                settings.<settings>.<telefoon>.Value = dialog.txtTelefoon.Text
                settings.Root.Add(<plaats></plaats>)
                settings.<settings>.<plaats>.Value = dialog.txtPlaats.Text
                settings.Root.Add(<logo></logo>)
                settings.<settings>.<logo>.Value = dialog.txtAfbeelding.Text
                'save
                settings.Save(settingsPath)
            End If

        End If

 
    End Sub

    Private Sub FormIsClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
        'Close the global connection so the db doesn't hang
        If Not dbh Is Nothing Then
            dbh.DBConnection.Close()
        End If
    End Sub

    Public Function showPersonForm() As Form
        'only create a new instance of the form if the forum does not yet exist
        If (_personForm Is Nothing OrElse _personForm.IsDisposed) Then
            _personForm = New frmPersons()
        End If
        _personForm.MdiParent = Me
        _personForm.Show()
        Return(_personForm)
    End Function

    Public Function showOrderForm() As Form
        If (_orderForm Is Nothing OrElse _orderForm.IsDisposed) Then
            _orderForm = New frmOrders()
        End If
        _orderForm.MdiParent = Me
        _orderForm.Show()
        Return _orderForm
    End Function

    Public Function showGiftListForm() As Form
        If (_giftListsForm Is Nothing OrElse _giftListsForm.IsDisposed) Then
            _giftListsForm = New frmGiftLists
        End If
        _giftListsForm.MdiParent = Me
        _giftListsForm.Show()
        Return _giftListsForm
    End Function

    Public Function showProductForm() As Form
        If (_productsForm Is Nothing OrElse _productsForm.IsDisposed) Then
            _productsForm = New frmProducts()
        End If
        _productsForm.MdiParent = Me
        _productsForm.Show()
        Return _productsForm
    End Function

  
    Private Sub ProductenToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ProductenToolStripMenuItem.Click, btnProducten.Click
        showProductForm()
    End Sub

    Private Sub AfsluitenToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AfsluitenToolStripMenuItem.Click
        Close()
    End Sub

    Private Sub GeschenkenlijstenToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GeschenkenlijstenToolStripMenuItem.Click, btnGeschenkenlijsten.Click
        showGiftListForm()
    End Sub

    Private Sub PersonenToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PersonenToolStripMenuItem.Click, btnPersonen.Click
        showPersonForm()
    End Sub

    Private Sub BestellingenToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BestellingenToolStripMenuItem.Click, btnBestellingen.Click
        showOrderForm()
    End Sub

    Private Sub HelpbestandToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles HelpbestandToolStripMenuItem.Click
        showHelp()
    End Sub
#Region "Help"
    Private ReadOnly Property HelpFile As String
        Get
            ' Return AppDomain.CurrentDomain.BaseDirectory 
            Return My.Application.Info.DirectoryPath & "\help\gifthelp.chm"
        End Get
    End Property

    Private Sub showHelp()

        Help.ShowHelp(Me, HelpFile, HelpNavigator.Index)
    End Sub
#End Region


End Class
